class Post {

    String title = ''
    String content = ''
    Date createTime = new Date()
    String needSex ='' // 需要教师的性别
    Student student

    static hasMany = [remarks:Remark,courses:String]

    static constraints = {
            title(size: 1..50,blank: true)
            needSex(blank: true)
            content(size: 1..1000,blank: true)
            student(nullable: true)
    }

    def shortTitle = {
        if (title?.size() > 10) {
            return title.substring(0,10)+'...'
        }
        return title
    }
        def shortContent = {
        if (content?.size() > 20) {
            return content.substring(0,20)+'...'
        }
        return content
    }

    String toStr() {
"""${title}
${content}
${student}
${needSex }
${createTime}"""
   }

      static namedQueries = { 
        random { max ->
            if (!max) {
                max = 1
            }
            def now = new Date() 
            lt 'createTime', now - (int)(Math.random()*100+1)
            maxResults(max)
            order("createTime", "asc")

        }
    }
}
